package com.lagou.edu.controller;

import com.lagou.edu.service.LoginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/login")
public class LoginController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }


    @GetMapping
    public String login() {
        return "/login/form";
    }

    @PostMapping
    public ModelAndView doLogin(ModelAndView mv, HttpSession session, String username, String password) {
        try {
            loginService.login(username, password, session);
            mv.setViewName("redirect:/resume/list");
        } catch (Exception e) {
            mv.addObject("message", e.getMessage());
            mv.setViewName("/login/form");
        }
        return mv;
    }
}
